package com.soul.dubbo.api.db;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.soul.dubbo.api.mapper.SettingsMapper;
import com.soul.model.db.Question;
import com.soul.model.db.Settings;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @author mdoya
 * @version 1.0
 * @description:
 */
@Slf4j
@DubboService
public class SettingsApiImpl implements SettingsApi{

    @Autowired
    private SettingsMapper settingsMapper;

    /**
     * 根据用户id查询通用设置
     * @param userId 用户id
     * @return
     */
    @Override
    public Settings findByUserSettings(Long userId) {
        // 方式一：
        LambdaQueryWrapper<Settings> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(userId != null,Settings::getUserId,userId);
        // 方式二：
//        QueryWrapper<Settings> wrapper = new QueryWrapper<>();
//        wrapper.eq("user_id",userId);
        return settingsMapper.selectOne(queryWrapper);
    }

    /**
     * 通知设置 - 保存(更新)
     *
     * @param settings
     * @return
     */
    @Override
    public void notificationsSetting(Settings settings) {
        Settings setting = findByUserSettings(settings.getUserId());
        if (setting != null){
            settings.setId(setting.getId());
            settingsMapper.updateById(settings);
            return;
        }
        settingsMapper.insert(settings);
    }


}
